package java_0530_map;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
@SuppressWarnings({"all"})
public class HashMapGround {
    public static void main(String[] args) {
        Map map = new HashMap();
        map.put("1","jack");
        map.put("2","rose");
        map.put("1","Elysia");


        /*
        1.执行构造器 new HashMap
          初始化加载因子 loadFactor = 0.75
            public HashMap() {
                this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
            }

        2.执行put,调用hash方法，计算key的hash值(
            return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);)

            public V put(K key, V value) {//K = "1" value = "jack"
                return putVal(hash(key), key, value, false, true);
            }

        3.执行putVal

            final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
                       boolean evict) {
                Node<K,V>[] tab; Node<K,V> p; int n, i;
                //如果底层的table数组为null，或者 length = 0，就扩容到16
                if ((tab = table) == null || (n = tab.length) == 0)
                    n = (tab = resize()).length;
                //找到hash值对应table的索引，如果里面为null，就new Node放入
                if ((p = tab[i = (n - 1) & hash]) == null)
                    tab[i] = newNode(hash, key, value, null);
                else {
                    Node<K,V> e; K k;//辅助变量
                    //如果table的索引位置的key的hash相同，且equals返回true
                    if (p.hash == hash &&
                        ((k = p.key) == key || (key != null && key.equals(k))))
                        e = p;
                    else if (p instanceof TreeNode)
                        e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
                    else {
                        //索引相同，对该索引下的链表进行循环比较
                        for (int binCount = 0; ; ++binCount) {
                            //没有相同的且下一个为空，将value放在链表最后
                            if ((e = p.next) == null) {
                                p.next = newNode(hash, key, value, null);
                                //在加入后判断一下要不要树化
                                if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
                                    treeifyBin(tab, hash);
                                break;
                            }
                            //如果有相同的就直接break
                            if (e.hash == hash &&
                                ((k = e.key) == key || (key != null && key.equals(k))))
                                break;
                            p = e;
                        }
                    }
                    if (e != null) { // existing mapping for key
                        V oldValue = e.value; //用新的value替换掉oldValue
                        if (!onlyIfAbsent || oldValue == null)
                            e.value = value;
                        afterNodeAccess(e);
                        return oldValue;
                    }
                }
                ++modCount;
                //threshold = capacity * load factor,扩容的临界值
                if (++size > threshold)
                    resize();
                afterNodeInsertion(evict);
                return null;
            }
         */


    }
}
